package dao;

import data.DataConnection;
import entity.WxUser;
import utils.DateUtil;

import java.util.List;



public class WxUserDao {

    /**

     * 根据主关键字删除微信用户

     * @param id 微信用户的主关键字

     * @return  成功返回true,否则返回false

     */

    public static boolean deleteByPrimaryKey(Integer id){

        //定义sql命令语句

        String sql="delete from wxuser where id=?";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{id};

        //判断命令执行是否正确，若正确返回true,否则返回false

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 向微信添加一个用户信息

     * @param wxUser 添加的微信实体

     * @return  添加成功返回true,否则返回false

     */

    public static boolean insert(WxUser wxUser){

        //定义sql命令语句

        String sql="insert into wxuser(" +

                "username," +

                "password," +

                "openid," +

                "sex," +

                "city," +

                "create_time," +

                "update_time)" +

                " values(?,?,?,?,?,?,?)";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{

                wxUser.getUsername(),

                wxUser.getPassword(),

                wxUser.getOpenid(),

                wxUser.getSex(),

                wxUser.getCity(),

                DateUtil.getSystemTime(),

                DateUtil.getSystemTime()

        };

        //判断命令执行是否正确，若正确返回true,否则返回false

        if(DataConnection.request(sql,para)){

            return true;

        }else{

            return false;

        }

    }



    /**

     * 通过主关键字id更新微信的的信息

     * @param wxUser 微信实体

     * @return  更新成功返回true,否则返回false

     */

    public static boolean updateByPrimaryKey(WxUser wxUser){

        //定义sql命令语句

        String sql="update wxuser set username=?," +

                "password=?," +

                "openid=?," +

                "sex=?," +

                "city=?," +

                "update_time=? " +

                " where id=?";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{wxUser.getUsername(),

                wxUser.getPassword(),

                wxUser.getOpenid(),

                wxUser.getSex(),

                wxUser.getCity(),

                DateUtil.getSystemTime(),

                wxUser.getId()

        };

        //判断命令执行是否正确，若正确返回true,否则返回false

        if(DataConnection.request(sql,para)){

            return true;

        }else{

            return false;

        }

    }



    /**

     * 根据id查找微信用户

     * @param id 是微信用户的主关键字

     * @return  若成功返回用户信息，否则返回null

     */

    public static WxUser selectByPrimaryKey(Integer id){

        //定义sql命令语句

        String sql ="select id," +

                "username," +

                "password," +

                "openid," +

                "sex=?," +

                "city=?," +

                "create_time," +

                "update_time   " +

                "  from wxuser " +

                "where id=? ";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{id};

        //判断命令执行是否正确，若正确返回用户信息,否则返回null

        List<WxUser> list=new DataConnection<WxUser>().query(WxUser.class,sql,para);

        if(list != null && list.size()>0){

            return list.get(0);

        }

        return null;

    }



    /**

     * 根据openid查找微信用户信息

     * @param openid  微信用户维一标号

     * @return  找到返回微信用户信息，否则返回null

     */

    public static WxUser findnameByopenId(String openid){

        //定义sql命令语句

        String sql="select id," +

                "username," +

                "password," +

                "openid," +

                "sex=?," +

                "city=?," +

                "create_time," +

                "update_time  " +

                " from  wxuser  " +

                " where openid=?";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{openid};

        List<WxUser> list=new DataConnection<WxUser>().query(WxUser.class,sql,para);

        //判断命令执行是否正确，若正确返回用户信息,否则返回null

        if(list != null && list.size()>0){

            return list.get(0);

        }else{

            return null;

        }

    }



    /**

     * 获取所有的用户

     */

    public static List<WxUser> wxUserList (){

        //定义sql命令语句

        String sql="select id," +

                "username," +

                "password," +

                "openid," +

                "sex=?," +

                "city=?," +

                "create_time," +

                "update_time  " +

                " from  wxuser  " ;

        //获取用户信息

        List<WxUser> list=new DataConnection<WxUser>().query(WxUser.class,sql,null);

        //判断命令执行是否正确，若正确返回用户信息,否则返回null

        if(list != null && list.size()>0){

            return list;

        }else{

            return null;

        }

    }



    /**

     * 根据用户名和密码查找用户是否存在

     * @param username  用户名

     * @param password  密码

     * @return  找到返回true,否则返回false

     */

    public static boolean checkUserByNameAndPassword(String username,String password){

        //定义sql命令语句

        String sql="select id," +

                "username," +

                "password," +

                "openid  " +

                "from xwuser  " +

                " where " +

                "username=?d" +

                " and " +

                "password=?";

        //给sql命令中的参数赋值。

        Object[] para=new Object[]{ username,password};

        //执行sql命令，查询用户信息

        List<WxUser> list=new DataConnection<WxUser>().query(WxUser.class,sql,para);

        //判断命令执行是否正确，若正确返回true,否则返回false

        if(list != null && list.size()>0){

            return true;

        }else{

            return false;

        }

    }

}